package com.recruit.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.recruit.common.LocalUser;
import com.recruit.common.mybatis.Page;
import com.recruit.common.util.PageUtil;
import com.recruit.model.ApplicationDO;
import com.recruit.mapper.ApplicationMapper;
import com.recruit.model.ResumeDO;
import com.recruit.model.UserDO;
import com.recruit.model.result.ApplicationResultDO;
import com.recruit.service.ApplicationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.recruit.service.ResumeService;
import com.recruit.vo.PageResponseVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Cjc
 * @since 2022-04-04
 */
@Slf4j
@Service
public class ApplicationServiceImpl extends ServiceImpl<ApplicationMapper, ApplicationDO> implements ApplicationService {

    @Resource
    private ApplicationMapper applicationMapper;
    @Autowired
    private ResumeService resumeService;

    @Override
    public boolean create(ApplicationDO applicationDO) {
        UserDO localUser = LocalUser.getLocalUser();
        if (localUser == null) {
            throw new RuntimeException("用户信息为空");
        }
        applicationDO.setUserId(localUser.getId());
        applicationDO.setState(0);

        LambdaQueryWrapper<ResumeDO> queryWrapper = Wrappers.<ResumeDO>lambdaQuery()
                .eq(ResumeDO::getUserId, localUser.getId());
        ResumeDO resumeDO = this.resumeService.getBaseMapper().selectOne(queryWrapper);
        if (resumeDO == null) {
            throw new RuntimeException("求职者简历信息不存在，请先填写简历！！！");
        }
        applicationDO.setResumeId(resumeDO.getId());

        return applicationMapper.insert(applicationDO) > 0;
    }

    @Override
    public PageResponseVO<ApplicationResultDO> getByHrId(Integer count,
                                                         Integer page,
                                                         Integer hrID,
                                                         Integer state) {
        Page<ApplicationResultDO> pager = new Page<>(page, count);
        IPage<ApplicationResultDO> paging = applicationMapper.getByHrId(pager, hrID, state);
        return PageUtil.build(paging);
    }

    @Override
    public boolean updateState(Integer id, Integer state) {
        return applicationMapper.updateState(id, state);
    }

    @Override
    public PageResponseVO<ApplicationResultDO> sort(Integer count, Integer page, Integer hrID) {
        Page<ApplicationResultDO> pager = new Page<>(page, count);
        IPage<ApplicationResultDO> paging = applicationMapper.sortByGrade(pager, hrID);
        return PageUtil.build(paging);
    }

    @Override
    public PageResponseVO<ApplicationResultDO> getByUserId(Integer count,
                                                           Integer page,
                                                           Integer userId) {
        Page<ApplicationResultDO> pager = new Page<>(page, count);
        IPage<ApplicationResultDO> paging = applicationMapper.getByUserId(pager, userId);
        return PageUtil.build(paging);
    }

}
